package knapsackProblem;

import java.util.UUID;
import org.joda.time.Duration;

import almm.State;


public  class Task implements State, Comparable<Task> {
	private UUID uuid;
	private Object task;
	private Balance balance;
	private Class<? extends Object> taskClass;
	public Duration getDuration() {
		return duration;
	}

	private Duration duration;
	public Task(Object task, Balance value,
			Duration duration) {
		this(task, value);
		this.duration = duration;
	}

	private Task(Object task, Balance value) {
		this.uuid = UUID.randomUUID();

		this.task = task;
		this.taskClass = task.getClass();
		this.balance = value;
	}

	public Object getTask() {
		return task;
	}

	public Class<? extends Object> getTaskClass() {
		return taskClass;
	}

	@Override
	public boolean equals(Object that) {
		return ((that instanceof Task) && ((Task) that).uuid.equals(this.uuid));
	}

	public int compareTo(Task that) {
			float comparsion = getBalanceToDuration(that.balance,
					that.duration) - getBalanceToDuration(this.balance,
					this.duration);
			int com = (comparsion == 0) ? 0 : (comparsion > 0) ? 1 : -1;
			return com;
	}

	private float getBalanceToDuration(Balance b, Duration d) {
		if (d.getMillis() == 0) {
			if (b.getValue() == 0)
				return 0;
			else
				return b.getValue() > 0 ? Float.POSITIVE_INFINITY
						: Float.NEGATIVE_INFINITY;
		}
		return (float) b.getValue() / (float) d.getMillis();
	}
}
